package ord2.impl.crawler;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import ord2.convert.Converter;
import ord2.convert.ConverterFactory;
import ord2.core.Parameter;
import ord2.core.Parametric;
import ord2.crawler.AbstractCrawler;
/**
 *
 * @author José Ivan
 * @author Edviges
 */
public final class DOUShellCrawler extends AbstractCrawler implements Parametric {

    private LinkedList<Parameter> pars = new LinkedList<Parameter>();
    private String pathShell;
    private String jornal;
    private String pathSave;

    public DOUShellCrawler(Parameter[] pars, int interval) {

        super(interval);
        this.pars.addAll(Arrays.asList(pars));
        for (Parameter p : pars) {

            if (p.getName().equals("shellPath")) {

                this.pathShell = p.getValue();
            } else if (p.getName().equals("jornal")) {

                this.jornal = p.getValue();
            } else if (p.getName().equals("savePath")) {

                this.pathSave = p.getValue();
            }
        }
    }

    @Override
    protected void processURL(URL url) throws Throwable {

        System.out.println("Starting crawler: " + this.jornal);
        //System.out.println(url.toString());
        Calendar c = Calendar.getInstance();
        int dia = c.get(Calendar.DAY_OF_MONTH);
        int mes = c.get(Calendar.MONTH) + 1;
        int ano = c.get(Calendar.YEAR);

        String sdia = (dia < 10
                        ?"0" + dia
                        :Integer.toString(dia));
        String smes = (mes < 10
                        ?"0" + mes
                        :Integer.toString(mes));
        String data = sdia + "/" + smes + "/" + ano;

        Process p = Runtime.getRuntime().exec(this.pathShell + " " + this.jornal
                        + " " + data + " " + this.pathSave); //+ " " + url.toString());
        p.waitFor();
        String datadir = this.pathSave + "/" + sdia + "_" + smes + "_" + ano + "_" + this.jornal
                + "/www.in.gov.br/servlet";
        File dir = new File(datadir);
        File[] f = dir.listFiles();
        Converter pdf_txt = ConverterFactory.getInstance().getAttachedInstance("pdf/txt");
        File txtdir = new File(this.pathSave + "/" + sdia + "_" + smes + "_"
                    + ano + "_" + this.jornal + "/txt/");
        if (!txtdir.exists()) {

            txtdir.mkdir();
        }
        
        for (File t : f) {

            File out = new File(this.pathSave + "/" + sdia + "_" + smes + "_"
                    + ano + "_" + this.jornal + "/txt/" + t.getName());
            try {
                
                pdf_txt.convert(t, out);
                this.notifyDocument(out);
            } catch (Exception e) {}
        }
    }

    @Override
    public Parameter[] getParameters() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addParameter(Parameter newParameter) {

        this.pars.add(newParameter);
    }

    @Override
    public void removeParameter(Parameter par) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
